package com.fengba.nanzhi.myinfo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by SpakeBrony on 2018/4/9.
 */

public class WxAccount extends AppCompatActivity {


    //微信号码
    private EditText WXH;

    //菊花
    private ProgressBar WXjuhua;

    public WxAccount() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_wx_account);
        WXH = (EditText)findViewById(R.id.WXH);
        WXjuhua = (ProgressBar) findViewById(R.id.WXjuhua);

        /**
         * 返回
         */
        ImageButton Wxbtn01 = (ImageButton)findViewById(R.id.Wxbtn01);
        Wxbtn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                //getActivity().overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
            }
        });


        /**
         * 保存
         */
        /*Button WXbtn02 = (Button) findViewById(R.id.WXbtn02);
        WXbtn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WXjuhua.setVisibility(ProgressBar.VISIBLE);
                    OkHttpClient client = new OkHttpClient();
                    //构建FormBody，传入要提交的参数
                    FormBody formBody = new FormBody
                            .Builder()
                            .add("id", "id")
                            .add("wechat", "wechat")
                            .build();
                    final Request request = new Request.Builder()
                            .url(UrlUtil.UrlFX + "/api/user/wechat")
                            .post(formBody)
                            .build();
                    Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            //ToastUtil.showToast(PostParameterActivity.this, "Post Parameter 失败");
                            ////System.out.println(e.getMessage());
                            ToastUtil.showToast(WxAccount.this,"网络错误",WXjuhua);

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            //final String responseStr = response.body().string();
                            //ToastUtil.showToast(PostParameterActivity.this, "Code：" + String.valueOf(response.code()));
                            try {
                                String json = response.body().string();
                                JSONObject jsonObject = new JSONObject(json);
                                final String message = (String) jsonObject.get("message");
                                //System.out.println("message " + message);
                                //System.out.println("jsonObject " + jsonObject);
                                if (jsonObject.get("code").equals(0)) {
                                    //JSONObject data = (JSONObject) jsonObject.get("data");
                                    *//**
                                     * 忘记密码
                                     *//*
                                    Intent intent = new Intent(WxAccount.this, com.fengba.nanzhi.fengba.MainActivity.class);
                                    //intent.putExtra("data", (Parcelable) data);
                                    startActivity(intent);
                                } else {
                                    ToastUtil.showToast(WxAccount.this,message,WXjuhua);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            }

        });*/
    }//都在这里打

    /**
     * 隐藏软键盘
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

}